package com.drcoffee.drcoffeemall.controller;

import com.drcoffee.drcoffeemall.entry.*;
import com.drcoffee.drcoffeemall.entry.requestBody.PartReq;
import com.drcoffee.drcoffeemall.service.AttachmentService;
import com.drcoffee.drcoffeemall.service.ExceptionRecordService;
import com.drcoffee.drcoffeemall.util.BodyToMapUtil;
import com.drcoffee.drcoffeemall.util.WeClientRequestEnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/part")
public class AttachmentController {
    @Autowired
    AttachmentService attachmentService;
    @Autowired
    ExceptionRecordService exceptionRecordService;


    /*
     * 添加配件
     * */
    @PostMapping("/insertParts")
    public ResponseEntity<Object> insertParts(@RequestParam("file") MultipartFile file, @RequestParam("file_name") String file_name, @RequestParam("part") String part) throws IOException {
        return attachmentService.insertParts(file, file_name, BodyToMapUtil.convertJsonToMap(part));
    }

    @PostMapping("/getAttachment")
    public Object getAttachment(@RequestBody PartReq partReq) {
        if (partReq.getData().equals("EN")) {
            String responseBody = WeClientRequestEnUtil.initiateGET("/getAttachment", partReq);
            Map<String, Object> responseMap = BodyToMapUtil.convertJsonToMap(responseBody);
            return new ResponseEntity<>(responseMap, HttpStatus.OK);
        } else {
            return attachmentService.getAttachment(partReq);
        }
    }

    @GetMapping("/getAttachmentById")
    public Map<String, Object> getAttachmentById(@RequestParam String language, @RequestParam Integer dot_id) {
        return attachmentService.getAttachmentById(language, dot_id);
    }

    @GetMapping("/getAttachmentAll")
    public ResponseEntity<Object> getAttachmentAll(@RequestParam String search, @RequestParam Integer currentPage, @RequestParam Integer pageSize) {
        return attachmentService.getAttachmentAll(search, currentPage, pageSize);
    }

    @PostMapping("/updImg")
    public Map<String, Object> updatepartimgurl(@RequestParam String part_code, @RequestParam String image_url) {
        return attachmentService.updatepartimgurl(image_url, part_code);
    }

    //同步OCM价格与配件至配件商城
    //设置定时作业，可手动执行
    @PostMapping("/refreshList")
    public Map<String, Object> refreshListFromOCM() {
        return refreshList();
    }

    public Map<String, Object> refreshList() {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            List<OCM2Mall> pricelist = attachmentService.getNewList();
            attachmentService.refreshList(pricelist);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("reason", e.toString());
        }
        return resultMap;
    }


    /*数据核对*/
    @GetMapping("/dataCheck")
    public ResponseEntity<Object> dataCheck() {
        return attachmentService.dataCheck();
    }


    @PostMapping("/getErpParts")
    public ResponseEntity<Object> getErpParts(@RequestBody Map<String,Object> paramMap) {
        return attachmentService.getErpParts(paramMap);
    }

    @PostMapping("/getErpInventory")
    public ResponseEntity<Object> getErpInventory(@RequestBody Map<String,Object> paramMap) {
        return attachmentService.getErpInventory(paramMap);
    }

}
